//
//  ZDBRechargeViewController.m
//  ZDB
//
//  Created by Kk on 2020/4/1.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBRechargeViewController.h"
#import "ZDBRechargeView.h"
#import "ZDBRechageModel.h"
@interface ZDBRechargeViewController ()
@property (nonatomic, strong) ZDBRechargeView * tableviewHeadView;//数据
@property (nonatomic, strong) ZDBRechargeView * tableviewHeadView1;//数据
@property (nonatomic, strong) ZDBRechargeView * tableviewHeadView2;//数据
@property (nonatomic, strong) ZDBRechargeView * tableviewHeadView3;//数据
@property (nonatomic, strong) ZDBRechargeView * tableviewHeadView4;//数据
@property (nonatomic, strong) NSString * registerUrl;//邀请注册二维码
@property (nonatomic,strong)UIScrollView * scrollerView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)UIImageView *bgimageView;
@property (nonatomic,strong)MJRefreshNormalHeader *header;
@property (nonatomic,strong)NSString * uid;
@end

@implementation ZDBRechargeViewController
- (void)viewWillAppear:(BOOL)animated
{
     [self statusBarStyleIsDark:NO];
     [self SetnavigationBarBackgroundImage:RGBA(253, 79, 43, 1) Alpha:1];
     [self SetNavigationBarTitleColor:[UIColor whiteColor]];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self statusBarStyleIsDark:YES];
    [self SetNavigationBarTitleColor:MAINTITLECOLOR];
    [self SetnavigationBarBackgroundImage:WHITECOLOR Alpha:1];
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title = @"充值任务详情";
     [self initBackButton:YES];
     [self.view addSubview:self.scrollerView];
     [_scrollerView addSubview:self.bgimageView];
    self.dataSource = [NSMutableArray new];
    _tableviewHeadView = [[NSBundle mainBundle] loadNibNamed:@"ZDBRechargeView" owner:self options:nil][0];
      _tableviewHeadView.frame = CGRectMake(0, 216, RealValue_W(345*2), RealValue_W(260));
      WeakSelf
      KViewRadius(_tableviewHeadView, 4);
      [_tableviewHeadView.receiveBtn add_BtnClickHandler:^(NSInteger tag) {//领取任务
          [weakSelf buttonClick:1];
      }];
      _tableviewHeadView.centerX =self.scrollerView.centerX;
      [self.scrollerView addSubview:self.tableviewHeadView];
      
      
      _tableviewHeadView1 = [[NSBundle mainBundle] loadNibNamed:@"ZDBRechargeView" owner:self options:nil][1];
      _tableviewHeadView1.frame = CGRectMake(0, 214, RealValue_W(345*2), 205);
      KViewRadius(_tableviewHeadView1, 4);
      [_tableviewHeadView1.urlCopyBtn add_BtnClickHandler:^(NSInteger tag) {//复制链接
          
           [weakSelf buttonClick:4];
      }];
      
      _tableviewHeadView1.centerX =self.scrollerView.centerX;
      [self.scrollerView addSubview:self.tableviewHeadView1];
      
      _tableviewHeadView2 = [[NSBundle mainBundle] loadNibNamed:@"ZDBRechargeView" owner:self options:nil][2];
      _tableviewHeadView2.frame = CGRectMake(0, 214, RealValue_W(345*2), 205);
      KViewRadius(_tableviewHeadView2, 4);
      [_tableviewHeadView2.subimtBtn add_BtnClickHandler:^(NSInteger tag) {//提交任务
          [weakSelf buttonClick:2];
      }];
      _tableviewHeadView2.centerX =self.scrollerView.centerX;
      [self.scrollerView addSubview:self.tableviewHeadView2];
      
      _tableviewHeadView3 = [[NSBundle mainBundle] loadNibNamed:@"ZDBRechargeView" owner:self options:nil][3];
      _tableviewHeadView3.frame = CGRectMake(0, 214, RealValue_W(345*2), 250);
      KViewRadius(_tableviewHeadView3, 4);
      _tableviewHeadView3.centerX =self.scrollerView.centerX;
      [self.scrollerView addSubview:self.tableviewHeadView3];
    
      _tableviewHeadView4 = [[NSBundle mainBundle] loadNibNamed:@"ZDBRechargeView" owner:self options:nil][4];
      _tableviewHeadView4.frame = CGRectMake(0, 214, RealValue_W(345*2), 270);
      KViewRadius(_tableviewHeadView4, 4);
    
      _tableviewHeadView4.centerX =self.scrollerView.centerX;
      [self.scrollerView addSubview:self.tableviewHeadView4];
     
      [self getRegisterTaskById];//获取3级任务详情
    // 上拉加载
   _header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page =1;
        [weakSelf selectTaskUserRechargeInfoByTurid];
    }];
    _tableviewHeadView3.tableView.mj_header = _header;
    
    MJRefreshBackStateFooter * foot = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page ++;
        [weakSelf selectTaskUserRechargeInfoByTurid];
    }];
    _tableviewHeadView3.tableView.mj_footer = foot;
}
#pragma 获取邀请好友注册列表
- (void)selectTaskUserRechargeInfoByTurid
{
    WeakSelf
    NSString  * selectTaskUserRechargeInfoByTurid = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"selectTaskUserRechargeInfoByTurid"];
    [[SmileHttpTool sharedInstance] POST:selectTaskUserRechargeInfoByTurid parameters:@{@"pageNum":@(self.page),@"pageSize":@(10),@"turid":_uid} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        NSDictionary * dic = responseObject;
        if (!kDictIsEmpty(dic)) {
            NSInteger  pages = [dic[@"pages"] integerValue];//一共多少条数据
            NSArray * list = dic[@"list"];//
            if (weakSelf.page>pages)
            {
                 [weakSelf.tableviewHeadView3.tableView.mj_footer endRefreshingWithNoMoreData];
            }else
            {
                if (weakSelf.page > 1)
                {
                    [weakSelf.dataSource addObjectsFromArray:[ZDBRechageModel mj_objectArrayWithKeyValuesArray:list]];
                    
                }else
                {
                    self.dataSource = [ZDBRechageModel mj_objectArrayWithKeyValuesArray:list];
                }
            }
            [self.tableviewHeadView3 reloadData:weakSelf.dataSource];
            
        }else
        {
            
            [JMNotifyView showNotify:message isSuccessful:NO];
        }
        [self.tableviewHeadView3.tableView.mj_header endRefreshing];
        [self.tableviewHeadView3.tableView.mj_footer endRefreshing];
       
    } failure:^(id responseObject, NSError *error) {
        
       [self.tableviewHeadView3.tableView.mj_header endRefreshing];
       [self.tableviewHeadView3.tableView.mj_footer endRefreshing];
        
    }];
}
#pragma 获取3级任务详情
- (void)getRegisterTaskById
{
    [self showLoadingAnimation];
    WeakSelf
    NSString  * getRechargeTaskById = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getRechargeTaskById"];
    [[SmileHttpTool sharedInstance] POST:getRechargeTaskById parameters:@{@"id":_id} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        if (statusCode==0) {
            NSString * sumPeople = kFormat(@"%@", responseObject[@"sumPeople"]) ;
            NSString * sumMoney = kFormat(@"%@", responseObject[@"sumMoney"]);
            NSString * already_invited_num =  kFormat(@"已邀请 %@ 人   累计佣金 %@",sumPeople,sumMoney);
            weakSelf.tableviewHeadView1.already_invited_num.attributedText =[ZBDUnit finderattributedString:already_invited_num attributedArray:@[kFormat(@"%@ 人", sumPeople),sumMoney] colors:@[MAINCOLOR,MAINCOLOR] font:AutoBoldFont(18)];
            weakSelf.tableviewHeadView1.already_invited_num.textAlignment =  NSTextAlignmentCenter;
            

            NSDictionary * taskRecharge = responseObject[@"taskRecharge"];//注册信息
             NSDictionary * taskUserRecharge = responseObject[@"taskUserRecharge"];//邀请信息
            NSString * taskUrl = taskRecharge[@"taskUrl"];
            [weakSelf.tableviewHeadView.icon sd_setImageWithURL:[NSURL URLWithString:taskUrl] placeholderImage:[UIImage imageNamed:@"Extension_Invitation"]];
            [weakSelf.tableviewHeadView2.icon sd_setImageWithURL:[NSURL URLWithString:taskUrl] placeholderImage:[UIImage imageNamed:@"Extension_Invitation"]];
            [weakSelf.tableviewHeadView3.icon sd_setImageWithURL:[NSURL URLWithString:taskUrl] placeholderImage:[UIImage imageNamed:@"Extension_Invitation"]];
            
            if (kDictIsEmpty(taskUserRecharge)) { //为空表示可领取任务 否是已经领取任务展示邀请信息
                
                 [weakSelf getTaskRegisterInfo:taskRecharge];
                
                
            }else
            {
                [weakSelf getTaskUserRecharge:taskUserRecharge];
            }
        }else
        {
            [JMNotifyView showNotify:message isSuccessful:NO];
            
        }
        [weakSelf stopLoadingAnimation];
        
    } failure:^(id responseObject, NSError *error) {
        [weakSelf stopLoadingAnimation];
        
    }];
}
#pragma 获取注册信息
- (void)getTaskRegisterInfo:(NSDictionary *)dic
{
    self.extensionType = RechargeReceiveType;
    [self getExtentWith:self.extensionType];
    _scrollerView.hidden = NO;
    _tableviewHeadView.titleLab.text =  kFormat(@"%@.分享充值链接", dic[@"title"]);
    NSString * reward = kFormat(@"%@%@", dic[@"rechargeRatio"],@"%");
    NSString * str = kFormat(@"可获得用户充值金额%@奖励",reward);
    
    _tableviewHeadView.rewardLab.attributedText =  [ZBDUnit finderattributedString:str attributedString:reward color:RechargeCLOLOR font:AutoFont(15)];
    
}
#pragma 获取充值信息
- (void)getTaskUserRecharge:(NSDictionary *)dic
{
    _scrollerView.hidden = NO;
    _registerUrl = dic[@"rechargeUrl"];//邀请注册地址
    
    NSString * reward = kFormat(@"%@%@", dic[@"rechargeRatio"],@"%");
    NSString * str = kFormat(@"可获得用户充值金额%@奖励",reward);
    _tableviewHeadView1.titleLab1.text =  kFormat(@"%@.分享充值链接", dic[@"title"]);
    _tableviewHeadView1.rewardLab1.attributedText =  [ZBDUnit finderattributedString:str attributedString:reward color:RechargeCLOLOR font:AutoFont(15)];
    self.extensionType = RechargeHaveInHandType;
    [self getExtentWith:self.extensionType];
    _uid = dic[@"uid"];
    [_header beginRefreshing];
     
    
}
//type =1 领取任务
- (void)buttonClick:(NSInteger)type
{
    if (![ZBDUnit getLoginJurisdiction]) {
        return;
    }
    switch (type) {
        case 1://领取任务
            {
             
                [self userReceiveRechargeTask];
            }
            break;
        case 2://提交任务
        {
            [self submitTaskRegisterTask];
        }
            break;
        case 4://复制链接
        {
            UIPasteboard * pastboard = [UIPasteboard generalPasteboard];
            pastboard.string = _registerUrl;
            [JMNotifyView showNotify:@"邀请链接已复制到粘贴板" isSuccessful:YES];
        }
            break;
            
            
        default:
            break;
    }
    
}
#pragma 领取三级任务
- (void)userReceiveRechargeTask
{
    [self showLoadingAnimation];
    WeakSelf
    NSString  * userReceiveRechargeTask = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"userReceiveRechargeTask"];
    [[SmileHttpTool sharedInstance] POST:userReceiveRechargeTask parameters:@{@"id":_id} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        if (statusCode==0) {
            
             NSDictionary * taskUserRecharge = responseObject[@"taskUserRecharge"];//邀请信息
            if (!kDictIsEmpty(taskUserRecharge)) {
                
                [weakSelf getTaskUserRecharge:taskUserRecharge];
             }
            if (self.taskSuccess) {
                self.taskSuccess();
            }
            
        }else
        {
            [JMNotifyView showNotify:message isSuccessful:NO];
        }
        [weakSelf stopLoadingAnimation];
        
    } failure:^(id responseObject, NSError *error) {
        [weakSelf stopLoadingAnimation];
        
    }];
   
}
#pragma 提交二级任务
- (void)submitTaskRegisterTask
{
    [self showLoadingAnimation];
       WeakSelf
       NSString  * submitTaskRegisterTask = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"submitTaskRegisterTask"];
       [[SmileHttpTool sharedInstance] POST:submitTaskRegisterTask parameters:@{@"id":_id} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
           
           if (statusCode==0) {
                [JMNotifyView showNotify:@"任务已提交" isSuccessful:NO];
               if (self.taskSuccess) {
                   self.taskSuccess();
               }
               [weakSelf.navigationController popViewControllerAnimated:YES];
               
               
           }else
           {
               [JMNotifyView showNotify:message isSuccessful:NO];
           }
           [weakSelf stopLoadingAnimation];
           
       } failure:^(id responseObject, NSError *error) {
           [weakSelf stopLoadingAnimation];
           
       }];
}
- (void)getExtentWith:(RechargeType)extensionType
{
    
    switch (extensionType) {
        case RechargeReceiveType:
        {
            _tableviewHeadView.hidden = _tableviewHeadView4.hidden = NO;
            _tableviewHeadView1.hidden =  _tableviewHeadView2.hidden = _tableviewHeadView3.hidden = YES;
            _tableviewHeadView.frame = CGRectMake(0, 214, RealValue_W(345*2), RealValue_W(260));
            _tableviewHeadView4.frame =  CGRectMake(0, _tableviewHeadView.bottom +20, RealValue_W(345*2), 270);
            _tableviewHeadView.centerX =  _tableviewHeadView4.centerX = self.scrollerView.centerX;
        }
            
            break;
        case RechargeHaveInHandType:
        {
            _tableviewHeadView1.hidden =  _tableviewHeadView3.hidden = _tableviewHeadView4.hidden = NO;
            _tableviewHeadView.hidden = _tableviewHeadView2.hidden = YES;
            _tableviewHeadView1.frame = CGRectMake(0, 214, RealValue_W(345*2), 205);
           
            _tableviewHeadView3.frame =  CGRectMake(0, _tableviewHeadView1.bottom +20, RealValue_W(345*2), 250);
            _tableviewHeadView4.frame =  CGRectMake(0, _tableviewHeadView3.bottom +20, RealValue_W(345*2), 270);
            _tableviewHeadView1.centerX =  _tableviewHeadView3.centerX = _tableviewHeadView4.centerX = self.scrollerView.centerX;
            [_scrollerView setContentSize:CGSizeMake(UIScreenWidth, _tableviewHeadView1.height + _tableviewHeadView3.height+ _tableviewHeadView4.height +214 +100)];
        }
            
            break;
        case RechargeOKType:
        {
             _tableviewHeadView2.hidden = _tableviewHeadView3.hidden = _tableviewHeadView4.hidden = NO;
            _tableviewHeadView.hidden = _tableviewHeadView1.hidden = YES;
            _tableviewHeadView2.frame = CGRectMake(0, 214, RealValue_W(345*2), 205);
            _tableviewHeadView3.frame =  CGRectMake(0, _tableviewHeadView2.bottom +20, RealValue_W(345*2), 250);
            _tableviewHeadView4.frame =  CGRectMake(0, _tableviewHeadView3.bottom +20, RealValue_W(345*2), 270);
            _tableviewHeadView2.centerX = _tableviewHeadView3.centerX = _tableviewHeadView4.centerX = self.scrollerView.centerX;
            
             [_scrollerView setContentSize:CGSizeMake(UIScreenWidth, _tableviewHeadView2.height + _tableviewHeadView3.height+ _tableviewHeadView4.height +214 +100)];
        }
            
            break;
            
        default:
            break;
    }
}
- (UIScrollView *)scrollerView
{
    if (!_scrollerView) {
        _scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight -kStatusBarAndNavigationBarHeight)];
        _scrollerView.backgroundColor = CLEARCOLOR;
        _scrollerView.showsVerticalScrollIndicator = NO;
        _scrollerView.showsHorizontalScrollIndicator = NO;
        _scrollerView.hidden = YES;
    }
    return _scrollerView;
}
- (UIImageView *)bgimageView
{
    if (!_bgimageView) {
        _bgimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, RealValue_H(732), RealValue_H(488))];
        _bgimageView.image = [UIImage imageNamed:@"imgcz1"];
        _bgimageView.centerX = _scrollerView.centerX;
    }
    return _bgimageView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
